package controllers

import (
	"TTSX_beego/models"
	"TTSX_beego/torch"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"math"
)

type AdminController struct {
	beego.Controller
}

func (This *AdminController) ShowAdminLogin() {

	adminName := This.Ctx.GetCookie("adminName")
	This.Data["adminName"] = adminName

	This.TplName = "admin/login.html"
}

func (This *AdminController) HandleAdminLogin() {
	userName := This.GetString("userName")
	pwd := This.GetString("password")
	remember := This.GetString("remember")
	if !torch.All(userName, pwd) {
		This.Ctx.WriteString("数据不完整")
		return
	}

	var user models.User
	o := orm.NewOrm()
	err := o.QueryTable("User").Filter("Name", userName).Filter("Power", 1).One(&user)
	if err != nil || pwd != user.PassWord {
		This.Ctx.WriteString("用户或者密码错误")
		return
	}

	if remember == "on" {
		This.Ctx.SetCookie("adminName", userName, 12*24*3600)
	} else {
		This.Ctx.SetCookie("adminName", "", -1)
	}

	This.SetSession("adminName", user)
	This.Redirect("/admin", 302)

}

func (This *AdminController) MoveAdmin() {
	This.Redirect("/admin/index", 302)
}

func (This *AdminController) ShowIndex() {

	page, err := This.GetInt("page")
	if err != nil {
		page = 1
	}

	selectType := This.GetString("select")

	var goods []models.GoodsSKU
	var types []models.GoodsType
	o := orm.NewOrm()
	o.QueryTable("GoodsType").All(&types)
	var count int64

	if selectType == "" {
		o.QueryTable("GoodsSKU").RelatedSel("GoodsType").RelatedSel("Goods").
			Limit(5, (page-1)*5).All(&goods)
		count, err = o.QueryTable("GoodsSKU").Count()
	} else {
		o.QueryTable("GoodsSKU").RelatedSel("GoodsType").RelatedSel("Goods").
			Limit(5, (page-1)*5).Filter("GoodsType__Name", selectType).All(&goods)
		count, err = o.QueryTable("GoodsSKU").Filter("GoodsType__Name", selectType).Count()
	}

	pageCount := int(math.Ceil(float64(count) / float64(5)))

	This.Data["goods"] = goods
	This.Data["pageCount"] = pageCount
	This.Data["pageIndex"] = page
	This.Data["count"] = count
	This.Data["types"] = types
	This.Data["typeName"] = selectType

	This.Layout = "admin/layout.html"
	This.TplName = "admin/index.html"
}

func (This *AdminController) ShowAddType() {

	o := orm.NewOrm()
	var GoodsTypes []models.GoodsType
	o.QueryTable("GoodsType").All(&GoodsTypes)
	This.Data["types"] = GoodsTypes

	This.Layout = "admin/layout.html"
	This.TplName = "admin/addType.html"
}

func (This *AdminController) HandleAddType() {

	typeName := This.GetString("typeName")
	logoPath := torch.UploadFile(&This.Controller, "uploadlogo", "/admin/addType")
	TypeImage := torch.UploadFile(&This.Controller, "uploadTypeImage", "/admin/addType")
	if !torch.All(typeName, logoPath, TypeImage) {
		This.Data["errmsg"] = "信息不完整"
		This.TplName = "/admin/add/Type"
		return
	}

	o := orm.NewOrm()
	var goodType = models.GoodsType{Name: typeName, Logo: logoPath, Image: TypeImage}
	o.Insert(&goodType)

	This.Redirect("/admin/addType", 302)

}

func (This *AdminController) ShowAddSPU() {
	o := orm.NewOrm()
	var goodsSpus []models.Goods
	o.QueryTable("Goods").All(&goodsSpus)
	This.Data["goodsSpus"] = goodsSpus

	This.Layout = "admin/layout.html"
	This.TplName = "admin/addGoodsSPU.html"
}

func (This *AdminController) HandleAddSPU() {
	spuName := This.GetString("spuName")
	spuDetail := This.GetString("spuDetail")
	if !torch.All(spuDetail, spuName) {
		This.Data["errmsg"] = "数据不完整"
		This.Layout = "admin/layout.html"
		This.TplName = "admin/addGoodsSPU.html"
		return
	}

	o := orm.NewOrm()
	spu := models.Goods{Name: spuName, Detail: spuDetail}
	o.Insert(&spu)
	This.Redirect("/admin/AddGoodsSPU", 302)

}

func (This *AdminController) ShowAddGoods() {

	o := orm.NewOrm()
	var types []models.GoodsType
	var spus []models.Goods
	o.QueryTable("GoodsType").All(&types)
	o.QueryTable("Goods").All(&spus)

	This.Data["types"] = types
	This.Data["goodsSPU"] = spus
	This.Layout = "admin/layout.html"
	This.TplName = "admin/add.html"
}

func (This *AdminController) HandleAddGoods() {
	selectGoodsSPU := This.GetString("selectGoodsSPU")
	selectType := This.GetString("selectType")
	goodsName := This.GetString("goodsName")
	desc := This.GetString("desc")

	goodsStock, err := This.GetInt("goodsStock")
	if err != nil {
		This.Data["errmsg"] = "必须是一个有效的库存数字"
		This.Layout = "admin/layout.html"
		This.TplName = "admin/add.html"
		return
	}

	goodsPrice, err := This.GetInt("goodsPrice")
	if err != nil {
		This.Data["errmsg"] = "必须是一个有效的价格数字"
		This.Layout = "admin/layout.html"
		This.TplName = "admin/add.html"
		return
	}

	fileName := torch.UploadFile(&This.Controller, "uploadname", "admin/add.html")
	if fileName == "" {
		This.Layout = "admin/layout.html"
		return
	}

	if !torch.All(selectGoodsSPU, selectType, goodsName, desc) {
		This.Layout = "admin/layout.html"
		This.TplName = "admin/add.html"
		This.Data["errmsg"] = "数据不完整"
		return
	}

	goodSPU := models.Goods{Name: selectGoodsSPU}
	goodType := models.GoodsType{Name: selectType}

	o := orm.NewOrm()
	err = o.Read(&goodType, "Name")
	if err != nil {
		This.Layout = "admin/layout.html"
		This.TplName = "admin/add.html"
		This.Data["errmsg"] = "类型错误，添加失败"
		return
	}
	err = o.Read(&goodSPU, "Name")
	if err != nil {
		This.Layout = "admin/layout.html"
		This.TplName = "admin/add.html"
		This.Data["errmsg"] = "类型错误，添加失败"
		return
	}

	goodSKU := models.GoodsSKU{Goods: &goodSPU, GoodsType: &goodType, Name: goodsName, Desc: desc, Price: goodsPrice,
		Image: fileName, Stock: goodsStock, Unite: "个"}

	o.Insert(&goodSKU)
	This.Redirect("/admin/AddGoods", 302)

}
